﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
//
namespace LinqToXmlDataBinding
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Linq;

    public partial class Window1 : Window
    {
        private Brush previousBrush;

        public Window1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Save MyFavorites list on closing.
        /// </summary>
        protected override void OnClosing(CancelEventArgs args)
        {
            var myFavorites = (XElement)((ObjectDataProvider)Resources["MyFavoritesList"]).Data;
            myFavorites.Save(@"..\..\data\MyFavorites.xml");
        }

        /// <summary>
        /// Add button event handler, adds currently selected video to MyFavorites
        /// </summary>
        private void OnAdd(object sender, EventArgs e)
        {
            var itemsList = (XElement)((ObjectDataProvider)Resources["MyFavoritesList"]).Data;
            itemsList.Add(videoListBox1.SelectedItem as XElement);
        }

        /// <summary>
        /// Delete button event handler, delets currently selected video from MyFavorites
        /// </summary>
        private void OnDelete(object sender, EventArgs e)
        {
            var selectedItem = (XElement)videoListBox2.SelectedItem;
            if (selectedItem != null)
            {
                if (selectedItem.PreviousNode != null)
                {
                    videoListBox2.SelectedItem = selectedItem.PreviousNode;
                }
                else if (selectedItem.NextNode != null)
                {
                    videoListBox2.SelectedItem = selectedItem.NextNode;
                }
                selectedItem.Remove();
            }
        }

        /// <summary>
        /// Searchbox event handler, Search videos by user specifed input
        /// </summary>
        private void OnKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key.Equals(Key.Enter))
            {
                var objectDataProvider = Resources["MsnVideosList"] as ObjectDataProvider;
                objectDataProvider.MethodParameters[0] = @"http://soapbox.msn.com/rss.aspx?searchTerm=" + searchBox.Text;
                objectDataProvider.Refresh();
            }
        }

        /// <summary>
        /// Change the color or the search links as the mouse enters and leaves to indicate
        /// that they are clickable
        /// </summary>
        private void OnMouseEnter(object sender, MouseEventArgs e)
        {
            var myLabel = sender as Label;
            previousBrush = myLabel.Foreground;
            myLabel.Foreground = Brushes.Blue;
        }

        private void OnMouseLeave(object sender, MouseEventArgs e)
        {
            var myLabel = sender as Label;
            myLabel.Foreground = previousBrush;
        }

        /// <summary>
        /// Event handlers for search options listed on the first page, simply update the static resource
        /// "MsnVideosList" with the new argument and refresh it.
        /// </summary>
        private void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            var content = (string)((Label)sender).Content;
            var objectDataProvider = Resources["MsnVideosList"] as ObjectDataProvider;

            switch (content)
            {
                case "Most Viewed":
                    objectDataProvider.MethodParameters[0] = @"http://soapbox.msn.com/rss.aspx?listId=MostPopular";
                    objectDataProvider.Refresh();
                    break;
                case "Most Recent":
                    objectDataProvider.MethodParameters[0] = @"http://soapbox.msn.com/rss.aspx?listId=MostRecent";
                    objectDataProvider.Refresh();
                    break;
                case "Top Favorites":
                    objectDataProvider.MethodParameters[0] = @"http://soapbox.msn.com/rss.aspx?listId=TopFavorites";
                    objectDataProvider.Refresh();
                    break;
                case "Top Rated":
                    objectDataProvider.MethodParameters[0] = @"http://soapbox.msn.com/rss.aspx?listId=TopRated";
                    objectDataProvider.Refresh();
                    break;
                case "My Favorites":
                    var msn = (XElement)objectDataProvider.Data;
                    var favorites = (XElement)((ObjectDataProvider)Resources["MyFavoritesList"]).Data;
                    msn.ReplaceAll(favorites.Elements("item"));
                    break;
            }
        }

        /// <summary>
        /// Play button event handler
        /// </summary>
        private void OnPlay(object sender, EventArgs e)
        {
            videoImage.Visibility = Visibility.Hidden;
            mediaElement.Play();
        }

        /// <summary>
        /// Stop button event handler
        /// </summary>
        private void OnStop(object sender, EventArgs e)
        {
            mediaElement.Stop();
            videoImage.Visibility = Visibility.Visible;
        }
    }
}